// 代码题一
let con = ' '
function Timeout(params) {
    return new Promise(function(resolve,reject){
        setTimeout(function(){
            con+=params
            resolve(con)
        },10)
      })
}
Timeout('hello').then(function name(res) {
    return Timeout('lago')
}).then(function name(res) {
    return Timeout('i love u')
}).then(function name(res) {
    console.log(res);
})

const { map } = require('lodash')
// 代码题二 练习1
const _ = require('lodash');
const fp = require('lodash/fp');
const cars = [
  {name:'aaa',horsepower:660,dollar_value:700000,in_stock:true},
  {name:'bbb',horsepower:650,dollar_value:648000,in_stock:false},
  {name:'ccc',horsepower:550,dollar_value:132000,in_stock:false},
  {name:'ddd',horsepower:525,dollar_value:114200,in_stock:false},
];
fp.flowRight(fp.last)
const result = fp.flowRight(fp.prop('in_stock'),fp.last);
// console.log(result(cars),'结果');

// 代码题二 练习2
const result2 = fp.flowRight(fp.prop('name'),fp.first);
// console.log(result2(cars),'结果2');

// 代码题二 练习3
let _average = function (xs) {
  return fp.reduce(fp.add,0,xs)/xs.length
}
let acverageDollarValue = function (val) {
  let result3 = fp.flowRight(_average,fp.map(value => value.dollar_value))
  return result3(val)
}
// console.log(acverageDollarValue(cars),'结果3');

// 代码题二 练习4
let testArr = ['HHH JJJJ KKKK','QQQQ WWWW EEEE']
let _underscore = fp.replace(/\W+/g,"_")
function sanitizeNames() {
  return fp.flowRight(fp.map(_underscore),fp.map(fp.toLower))
}
// console.log(sanitizeNames()(testArr),'结果4');


// 代码题三 练习1
class Container{
  static of(value){
      return new Container(value)
  }
  constructor(value){
      this._value = value
  }
  map(fn){
      return Container.of(fn(this._value))
  }
}
class MayBe{
  static of(x){
      return new MayBe(x)
  }
  isNothing(){
      return this._value === null || this._value === undefined
  }
  constructor(x){
      this._value = x
  }
  map(fn){
      return this.isNothing()?this:MayBe.of(fn(this._value))
  }
}
let maybe = MayBe.of([5,6,1]);
let ex1 = (value) =>{
  let fn = fp.flowRight(fp.map(fp.add(value)))
  return maybe.map(fn)
}
console.log(ex1(1),'代码三练习1');

// 代码题三 练习2
let xs = Container.of(['do','ray','me','fa','so','la','ti','do'])
let ex2 = (value) => {
    return xs.map(fp.first)._value
}
console.log(ex2(),'代码三练习2');

// 代码题三 练习3
let safeProp = fp.curry(function (x,o) {
  return MayBe.of(o[x])
})
let user ={id:2,name:'Albert'};
let ex3 = (value) =>{
  return safeProp('name',user).map(fp.first)._value
}
console.log(ex3(),'代码三练习3');

// 代码题三 练习4
let  ex4 = function(n){
  let m1 = new MayBe(n)
  let m2 = m1.map(parseInt)
  return m2._value
}
console.log(ex4('233333啊实打实'),'代码三练习4');



